function OS_Battle(host,id)
{
	this.$super=OS_Interface;
	if (this.$super(host,id)===false) return;
	delete this.$super;
	
	var parent=null;
	var background=null;
	this.bgm=null;
	
	var activeSkill=null;
	var activeBattler=null;
	var activeTarget=null;
	var enemys=[];
	var partners=[];
	var step=null;
	var stepStartTime=0;
	
	var enemyPositions=[[340,160],[250,70],[500,190]];
	var partnerPositions=[[150,320],[30,300],[230,400]];
	
	var activeDialog=null;
	
	var selectPointer=-1;
	var selectSide=-1;
	
	var effects=[];   //特效
	
	var probability=0.6;
	
	var victory=false;
	
	var victoryTime;
	
	var totalExperience;
	
	this.putEffect=function(frame,left,top)   //特效导入
	{
		effects.push({frame:frame,left:left,top:top});
	}

	var instance=this;         //战斗实例
	
	this.setActiveDialog=function(dialog)  // 活动窗口
	{
		activeDialog=dialog;
	}
	
	this.load=function(handler)  //初始化装载
	{
		var children=this.resourceNode.childNodes;
		for (var i=0;i<children.length;i++)
		{
			if (children[i].nodeType!=1) continue;
			var child=children[i];
			switch(child.nodeName)
			{
				case "background":
					var imageid=child.childNodes[0].nodeValue;
					background=OS_Resource.resource.get(imageid);
					break;
				case "bgm":
					var musicid=child.childNodes[0].nodeValue;
					this.bgm=OS_Resource.resource.get(musicid);
					break;
				case "enemyGroup":
					var enemyList=child.childNodes;
					for (j=0;j<enemyList.length;j++)
					{
						if (enemyList[j].nodeType!=1) continue;
						var enemyNode=enemyList[j];
						var enemy={battler:OS_Resource.resource.get(enemyNode.childNodes[0].nodeValue)};
						enemys.push(enemy);
					}
					break;
			}
		}
		this.loaded=true;
		handler(this);
	}
	
	this.changeStep=function(s)  //移换步骤
	{
		step=s;
		stepStartTime=(new Date()).getTime();
	}
	
	this.initEnemys=function() //初始化敌人队伍
	{
		var flag=0;
		if (enemys.length==2) flag=1;
        totalExperience=0;
		for (var i=0;i<enemys.length;i++)
		{
			var enemy=enemys[i];
			for (index in enemy.battler)
			{
				enemy[index]=enemy.battler[index];
				
			}
			enemy.hp=enemy.maxHp;
			enemy.mp=enemy.maxMp;
			enemy.currentFrame=enemy.actor.actions["standf"];
			enemy.alive=true;
			enemy.activePoint=0;
			enemy.faceto="down";
			enemy.careful=false;
			enemy.borderSize=0;
			enemy.borderLimit=68;
			enemy.states=[];
			enemy.side="enemy";
			if (i<enemyPositions.length)
			{
				enemy.position={x:enemyPositions[i+flag][0],y:enemyPositions[i+flag][1]};
				enemy.currentPosition={x:enemyPositions[i+flag][0],y:enemyPositions[i+flag][1]}
			}
			totalExperience+=enemy.experience;
		}
	}
	
	this.initPartners=function()  //初始化主角队伍
	{
		var flag=0;
		var count=0;
		victory=false;
		if (host.status.team.realNumber()==2) flag=1;
		
		partners=[];
		for (var i=0;i<host.status.team.members.length;i++)
		{
			var member=host.status.team.members[i];
			if (!member.appearance) continue;
			var attrs=[
						"speed","initialSpeed","speedAddition",
						"attack","initialAttack","attackAddition",
						"defense","initialDefense","defenseAddition",
						"magic","initialMagic","magicAddition",
						"resistance","initialResistance","resistanceAddition"
					];
			for (var j=0;j<attrs.length;j+=3)
			{
				member[attrs[j]]=member[attrs[j+1]]+(member.level-1)*member[attrs[j+2]];
			}
			member.actor=member.battler.actor;
			member.currentFrame=member.actor.actions["standb"];
			member.normalAttackSkill=member.battler.normalAttackSkill;
			member.specialSkills=member.battler.specialSkills;
			member.alive=true;
			member.activePoint=0;
			
			member.faceto="up";
			member.careful=false;
			member.borderSize=0;
			member.borderLimit=68;
			member.states=[];
			member.mp=member.maxMp;
			member.hp=member.maxHp;
			member.upgrade=false;
			member.side="partner";
			if (count+flag<partnerPositions.length)
			{
				member.position={x:partnerPositions[count+flag][0],y:partnerPositions[count+flag][1]};
				member.currentPosition={x:partnerPositions[count+flag][0],y:partnerPositions[count+flag][1]};
			}
			count++;
			partners.push(member);
		}
	}
	
	//更新各战斗角色的活动点数
	this.updateActivePoint=function()  //检测换谁攻击
	{
		for (var i=0;i<enemys.length;i++)
		{
			if (!enemys[i].alive) continue;
			if (enemys[i].activePoint>=1000)
			{
				enemys[i].activePoint=-1;
				activeBattler=enemys[i];    
				this.changeStep("chooseAction");
				return;
			}
		}
		for (var i=0;i<partners.length;i++)
		{
			if (!partners[i].alive) continue;
			if (partners[i].activePoint>=1000)
			{
				partners[i].activePoint=-1;
				activeBattler=partners[i];
				this.changeStep("chooseAction");
				return;
			}
		}
		for (var i=0;i<enemys.length;i++)
		{
			if (!enemys[i].alive) continue;
			enemys[i].activePoint+=enemys[i].speed*host.delayTime/100;
			if (enemys[i].activePoint>1000) enemys[i].activePoint=1000;
		}
		for (var i=0;i<partners.length;i++)
		{
			if (!partners[i].alive) continue;
			partners[i].activePoint+=partners[i].speed*host.delayTime/100;
			if (partners[i].activePoint>1000) partners[i].activePoint=1000;
		}
	}
	
	this.$show=this.show;
	this.show=function()
	{
		parent=host.status.activeInterface; //活动主画面
		this.$show();
		host.studio.changeBGM([this.bgm]);
		this.initEnemys();
		this.initPartners();
		this.changeStep("updateActivePoint");
	}
	
	this.over=function()                    //战斗结束        
	{
		parent.resume();           
		host.status.activeInterface=parent;
		host.studio.changeBGM([parent.bgm]);
		if (!!this.callback) this.callback();
	}
	
	var getAliveEnemyIndex=function(isRandom)   //敌人是否随机选择进行攻击
	{
		var aliveList=[];
		for (var i=0;i<enemys.length;i++)
		{
			if (enemys[i].alive)
			{
				aliveList.push(i);
			}
		}
		if (!!isRandom)
		{
			var index=Math.floor(Math.random()*aliveList.length);
			return aliveList[index];
		}
		else if (aliveList.length>0)
		{
			return aliveList[0];
		}
		else
		{
			return false;
		}
	}
	
	var getAlivePartnerIndex=function(isRandom)  //主角是否随机选择进行攻击
	{
		var aliveList=[];
		for (var i=0;i<partners.length;i++)
		{
			if (partners[i].alive)
			{
				aliveList.push(i);
			}
		}
		if (!!isRandom)
		{
			var index=Math.floor(Math.random()*aliveList.length);
			return aliveList[index];
		}
		else if (aliveList.length>0)
		{
			return aliveList[0];
		}
		else
		{
			return false;
		}
	}
	
	var getAliveEnemys=function()
	{
		var aliveList=[];
		for ( var i=0;i<enemys.length;i++)
		{
			if(enemys[i].alive)
			{
				aliveList.push(enemys[i]);
				
			}
		}
		return aliveList;
	}
	
	var getAlivePartners=function()
	{
		var aliveList=[];
		for(var i=0;i<partners.length;i++)
		{
			if(partners[i].alive)
			{
				aliveList.push(partners[i]);
			}
		}
		return aliveList;
	}
	
	this.update=function()
	{
		switch(step)
		{
			case "updateActivePoint":                   //更新活动点数
				var enemyAlive=getAliveEnemyIndex();
				var partnerAlive=getAlivePartnerIndex();
				if (enemyAlive===false)
				{
					this.changeStep("victory");
				}
				else if (partnerAlive===false)
				{
					this.changeStep("defeated");
				}
				else
				{
					this.updateActivePoint();
				}
				break;
			case "chooseSkill":                      //选择技能
				if (activeDialog==null)
				{
					var params={};
					params.position="center";
					params.width=180;
					params.height=180;
					params.text="技能";
					params.key="$battleChooseSkill_val";
					params.options=[];
					for (var i=0;i<activeBattler.specialSkills.length;i++)
					{
						var skill=activeBattler.specialSkills[i];
						var content={text:skill.name,value:i};
						if(skill.cost>activeBattler.mp)
						{
							content={text:skill.name,value:i,disable:true};
						}
						params.options.push(content);
					}
					params.callback=function()
					{
						var actionChoosed=host.status.V.get(params.key);
						activeSkill=activeBattler.specialSkills[actionChoosed];
						var myside=0;
						if (activeSkill.selectCount=="single")
						{
							selectPointer=getAliveEnemyIndex();
						}
						else
						{
							selectPointer="all";
						}
						if (activeSkill.selectTarget=="enemy")
						{
							selectSide=1-myside;
						}
						else
						{
							selectSide=myside;
						}
						instance.changeStep("chooseTargetToUseSkill");
					}
					activeDialog=new OS_SelectDialog(host,this,parent.getWinRes(),params);
					activeDialog.open();
				}
				else
				{
					if (!!host.status.checkKeyPressed("esc"))
					{
						activeDialog.setCallback(function(){
							instance.changeStep("chooseAction");
						});
						activeDialog.close();
					}
					else
					{
						activeDialog.interceptKey();
						activeDialog.update();
					}
				}
				break;
			case "chooseTargetToUseSkill": 			//选择目标使用技能
			    if (activeBattler.side=="partner")
				{
				   var selectCount=partners.length;
				   if (selectSide==1) selectCount=enemys.length;
				   var upKey=host.status.checkKeyPressed("up");
				   var leftKey=host.status.checkKeyPressed("left");
				   var downKey=host.status.checkKeyPressed("down");
				   var rightKey=host.status.checkKeyPressed("right");
				   var enterKey=host.status.checkKeyPressed("enter");
				   var escKey=host.status.checkKeyPressed("esc");
				   var spaceKey=host.status.checkKeyPressed("space");
				   if (!!enterKey || !!spaceKey)
				   {
					  activeBattler.activePoint=0;
					  if (selectPointer!="all")
					   {
						  activeTarget=[selectSide==0?partners[selectPointer]:enemys[selectPointer]];
					   }
					   else
					   {
						  if (selectSide==1) activeTarget=getAliveEnemys();
						  else activeTarget=getAlivePartners();
				     	}
					       selectPointer=-1;
					       selectSide=-1;
						   activeBattler.mp-=activeSkill.cost;
					       this.changeStep("updateSkill");
				    }
				    else if (!!escKey)
				    {
					    selectPointer=-1;
					    selectSide=-1;
					    this.changeStep("chooseSkill");
				    }
				    else if (selectPointer=="all")
				    {}
				    else if (!!upKey || !!leftKey)
				   {
					   var targetsTemp=selectSide==0?partners:enemys;
					  do{
						  selectPointer=(selectPointer-1+selectCount)%selectCount;
					  }while(!targetsTemp[selectPointer].alive);
				   }
				    else if (!!downKey || !!rightKey)
				   {
					   var targetsTemp=selectSide==0?partners:enemys;
					   do{
						   selectPointer=(selectPointer+1)%selectCount;
					   }while(!targetsTemp[selectPointer].alive);
				   }        
				            
							    var	stateList=activeBattler.states;
								var len=stateList.length;
								var count=0;
								var index=0;
							    while(count<len)
							   {
									stateList[index].setNum(stateList[index].getNum()-1);
									if(stateList[index].getNum()==0)
									 {
									 stateList.splice(index,1);
									 }
									 else
									 {
									 index++;
									 }	 
								count++;
						       } 
				    
				}
				else       //怪物使用技能
				{
				   
					activeSkill=activeBattler.specialSkills[Math.floor(Math.random()*activeBattler.specialSkills.length)];
					if(!activeSkill)
					{
						
						this.changeStep("chooseTargetToAttack");
						break;
					}
					activeBattler.activePoint=0;
					
				    var stateList=activeBattler.states;
					var state=null;
					
				    for(var i=0;i<stateList.length;i++)          //判断是否有混沌状态
					{
					   if(stateList[i].test("chaos"))
					    {
					     state="chaos";
					     break;
						}
					}
					if(!!state)
					{
						if(activeSkill.selectCount=="single")
						{
							 var indexRandom=getAliveEnemyIndex(true);
						     activeTarget=[enemys[indexRandom]]; 
							
						}
						else
						{
							activeTarget=getAliveEnemys();
					  
						}
						 
					}
					else
					{
						if(activeSkill.selectCount=="single")
						{
							
							 var indexRandom=getAlivePartnerIndex(true);
					         activeTarget=[partners[indexRandom]];
						}
						else
						{
							activeTarget=getAlivePartners();
						}
					     
					}
					      
								stateList=activeBattler.states;
								var len=stateList.length;
								var count=0;
								var index=0;
								  while(count<len)
								 {
									stateList[index].setNum(stateList[index].getNum()-1);
									if(stateList[index].getNum()==0)
									 {
									 stateList.splice(index,1);
									 }
									 else
									 {
									 index++;
									 }	 
								count++;
								 }
							
					    this.changeStep("updateSkill");    
				
				}
				  break;
			case "chooseAction":                  //选择方法
				if (activeBattler.side=="partner")
				{
				    
					var state=null;
				    var stateList=activeBattler.states;
					
				    for(var i=0;i<stateList.length;i++)          //判断是否有睡眠状态
				       {
					      if(stateList[i].test("sleep"))
					        {
					                state="sleep";
					                break;
						    }
					   }
				    if(!!state)
					     {
						 
								stateList=activeBattler.states;
								var len=stateList.length;
								var count=0;
								var index=0;
								  while(count<len)
								 {
									stateList[index].setNum(stateList[index].getNum()-1);
									if(stateList[index].getNum()==0)
									 {
									 stateList.splice(index,1);
									 }
									 else
									 {
									 index++;
									 }	 
								count++;
						       } 
						   
						 activeBattler.activePoint=0;
					     this.changeStep("updateActivePoint");
					     break;
					     }
					     
				    for(var i=0;i<stateList.length;i++)          //判断是否有混沌状态
				       {
					      if(stateList[i].test("chaos"))
					        {
					                state="chaos";
					                break;
						    }
					   }
				    if(!!state)
					     {
					     this.changeStep("chooseTargetToAttack");
					     break;
					     }
					
					if (activeDialog==null)
					{
						  for(var i=0;i<stateList.length;i++)          //判断是否有封锁技能状态
				         {
					       if(stateList[i].test("block"))
					        {
					                state="block";
					                break;
						    }
					     }
						
						var params={};
						params.position="center";
						params.width=140;
						params.height=150;
						params.text=activeBattler.name;
						params.key="$battleChooseAction_val";
						if(!state)
						{
							params.options=[{text:"  攻击  ",value:"攻击"},{text:"  技能  ",value:"技能"},{text:"  防御  ",value:"防御"}];                  }
					    else
						{    
						params.options=[{text:"  攻击  ",value:"攻击"},{text:"  技能  ",value:"技能",disable:true},{text:"  防御  ",value:"防御"}];
						}
						params.callback=function()
						{
							var actionChoosed=host.status.V.get(params.key);
							switch(actionChoosed)
							{
								case "攻击":
									activeSkill=activeBattler.normalAttackSkill;
									var myside=0;
									if (activeSkill.selectCount=="single")
									{
										selectPointer=getAliveEnemyIndex();
									}
									else
									{
										selectPointer="all";
									}
									if (activeSkill.selectTarget=="enemy")
									{
										selectSide=1-myside;
									}
									else
									{
										selectSide=myside;
									}
									instance.changeStep("chooseTargetToAttack");
									break;
								case "技能":
									instance.changeStep("chooseSkill");
									break;
								case "防御":
									stateList=activeBattler.states;
									var len=stateList.length;
									var count=0;
									var index=0;
									while(count<len)
									{
										stateList[index].setNum(stateList[index].getNum()-1);
										if(stateList[index].getNum()==0)
										{
											stateList.splice(index,1);
										}
										else
										{
											index++;
										}	 
										count++;
									}
									
									var state=new OS_BattlerState();
									state.setName("defense");
									state.setNum(1);
									activeBattler.states.push(state);
									activeBattler.activePoint=200;
									
									instance.changeStep("updateActivePoint");
									break;
							}
						}
						activeDialog=new OS_SelectDialog(host,this,parent.getWinRes(),params);
						activeDialog.open();
					}
					else
					{
						activeDialog.interceptKey();
						activeDialog.update();
					}
				}
				//怪物进攻
				else
				{
						  var ranNum=Math.floor(Math.random()*10);
						  var state=null;
						  var stateList=activeBattler.states;
						  
					      for(var i=0;i<stateList.length;i++)          //判断是否有睡眠状态
				          {
					        if(stateList[i].test("sleep"))
					        {
					                state="sleep";
					                break;
						    }
					      }
					      if(!!state)
					     {
						   
								stateList=activeBattler.states;
								var len=stateList.length;
								var count=0;
								var index=0;
								  while(count<len)
								 {
									stateList[index].setNum(stateList[index].getNum()-1);
									if(stateList[index].getNum()==0)
									 {
									 stateList.splice(index,1);
									 }
									 else
									 {
									 index++;
									 }	 
								count++;
						       } 
						 activeBattler.activePoint=0;
					     this.changeStep("updateActivePoint");
					     break;
					     }
						  
						  for(var i=0;i<stateList.length;i++)          //判断是否有封锁技能状态
					      {
					          if(stateList[i].test("block"))
					         {
					            state="block";
					            break;
						     }
					      }
			              if(ranNum<10*probability&&!state)
						  {
						   
						  this.changeStep("chooseTargetToUseSkill");
						  }
						  else
						  {
						  this.changeStep("chooseTargetToAttack");
						  }
				}
				break;
			case "chooseTargetToAttack":    //选择目标进行攻击
			    if (activeBattler.side=="partner")
				{
					var  state=null;
					var  stateList=activeBattler.states;
					activeSkill=activeBattler.normalAttackSkill;
				    for(var i=0;i<stateList.length;i++)          //判断是否有混沌状态
				          {
					        if(stateList[i].test("chaos"))
					        {
					                state="chaos";
					                break;
						    }
					      }
					if(!!state)
					{
				      
								stateList=activeBattler.states;
								var len=stateList.length;
								var count=0;
								var index=0;
								  while(count<len)
								 {
									stateList[index].setNum(stateList[index].getNum()-1);
									if(stateList[index].getNum()==0)
									 {
									 stateList.splice(index,1);
									 }
									 else
									 {
									 index++;
									 }	 
								count++;
						       } 
						   
						var indexRandom=getAlivePartnerIndex(true);
						activeTarget=[partners[indexRandom]];
						this.changeStep("updateSkill");     
						break; 
						   
					}
					     
				 
				   var selectCount=partners.length;
				   if (selectSide==1) selectCount=enemys.length;
				   var upKey=host.status.checkKeyPressed("up");
				   var leftKey=host.status.checkKeyPressed("left");
				   var downKey=host.status.checkKeyPressed("down");
				   var rightKey=host.status.checkKeyPressed("right");
				   var enterKey=host.status.checkKeyPressed("enter");
				   var escKey=host.status.checkKeyPressed("esc");
				   var spaceKey=host.status.checkKeyPressed("space");
				   if (!!enterKey || !!spaceKey)
				   {
					   activeBattler.activePoint=0;
					   if (selectPointer!="all")
					  {
						  activeTarget=[selectSide==0?partners[selectPointer]:enemys[selectPointer]];
					  }
				      else
				      {
						if (selectSide==1) activeTarget=enemys;
						else activeTarget=partners;
					  }
					  selectPointer=-1;
					  selectSide=-1;
					  this.changeStep("updateSkill");
				    }
				    else if (!!escKey)
				   {
					selectPointer=-1;
					selectSide=-1;
					this.changeStep("chooseAction");
				   }
				   else if (selectPointer=="all")
				   {}
				   else if (!!upKey || !!leftKey)
				   {
					  do{
						  selectPointer=(selectPointer-1+selectCount)%selectCount;
					  }while(!enemys[selectPointer].alive);
				   }
				   else if (!!downKey || !!rightKey)
				   {
					  do{
						  selectPointer=(selectPointer+1+selectCount)%selectCount;
					  }while(!enemys[selectPointer].alive);
				   }
				  
				    
								stateList=activeBattler.states;
								var len=stateList.length;
								var count=0;
								var index=0;
								  while(count<len)
								 {
									stateList[index].setNum(stateList[index].getNum()-1);
									if(stateList[index].getNum()==0)
									 {
									 stateList.splice(index,1);
									 }
									 else
									 {
									 index++;
									 }	 
								count++;
						       } 
				}
				else //攻击方为怪物
				{
				  
					activeBattler.activePoint=0;
					activeSkill=activeBattler.normalAttackSkill;
					
				    var stateList=activeBattler.states;
					var state=null;
					
				    for(var i=0;i<stateList.length;i++)          //判断是否有混沌状态
					{
					   if(stateList[i].test("chaos"))
					    {
					     state="chaos";
					     break;
						}
					}
					if(!!state)
					{
						var indexRandom=getAliveEnemyIndex(true);
						activeTarget=[enemys[indexRandom]]; 
						this.changeStep("updateSkill");   
						
							 
					}
					else
					{
						var indexRandom=getAlivePartnerIndex(true);
						activeTarget=[partners[indexRandom]];
						this.changeStep("updateSkill");    
					}
						
					 
							stateList=activeBattler.states;
						    var len=stateList.length;
						    var count=0;
						    var index=0;
							  while(count<len)
							 {
								stateList[index].setNum(stateList[index].getNum()-1);
								if(stateList[index].getNum()==0)
								 {
								 stateList.splice(index,1);
								 }
								 else
								 {
								 index++;
								 }	 
							 count++;
							 } 
						
					
				}
				break;
			case "updateSkill":    //更新技能动画
				var finish=activeSkill.update(this,activeBattler,activeTarget,stepStartTime);
				if (!!finish)
				{
					this.changeStep("updateActivePoint");
					activeSkill=null;
					activeTarget=null;
				}
				break;
			case "victory":
			        if(!victory)    //防止多次计算
			       {
					   for(var k=0;k<partners.length;k++)
				     {
			           
					 	 partners[k].experience+=totalExperience;
						 while(partners[k].experience>=partners[k].experienceLimit)
						 {
							   partners[k].experienceLimit*=partners[k].experienceAddition;
							   if(partners[k].level<partners[k].maxlevel)
							   {
								   partners[k].level+=1;
								   partners[k].upgrade=true;
								   
								   partners[k].maxHp+=partners[k].hpAddition;
								   partners[k].hp=partners[k].maxHp;
								   partners[k].maxMp+=partners[k].mpAddition;
								   partners[k].mp=partners[k].maxMp;
								   
							   }
							 
						 }
					   
				     }
					   
					   victoryTime=(new Date()).getTime();
					   victory=true;
					   
				   }
				for (var i=0;i<partners.length;i++)
				{  
					partners[i].currentFrame=partners[i].actor.actions["victory"];
					if (!partners[i].alive)
					{
						partners[i].hp=1;
						partners[i].alive=true;
					}
				}
				if (
					host.status.checkKeyPressed("esc")||
					host.status.checkKeyPressed("enter")||
					host.status.checkKeyPressed("space")||
					(new Date()).getTime()-stepStartTime>5000
					)
				{
					host.status.V.put("$OutcomeOfTheBattle","victory");
					this.over();
				}
				break;
			case "defeated":
				for (var i=0;i<partners.length;i++)
				{
					if (!partners[i].alive)
					{
						partners[i].hp=1;
					}
				}
				for (var i=0;i<enemys.length;i++)
				{
					enemys[i].currentFrame=enemys[i].actor.actions["victory"];
				}
				if (
					host.status.checkKeyPressed("esc")||
					host.status.checkKeyPressed("enter")||
					host.status.checkKeyPressed("space")||
					(new Date()).getTime()-stepStartTime>5000
					)
				{
					host.status.V.put("$OutcomeOfTheBattle","defeated");
					this.over();
				}
				break;
		}
	}
	
	this.drawBattler=function(battler)  //绘制怪物
	{
		if (!battler.alive)
		{
			var suffix="";
			if (battler.faceto=="down") suffix="f";
			else suffix="b";
			battler.currentFrame=battler.actor.actions["dead"+suffix];
		}
		var action=null;
		var barWidth=64;
		var barTop=100;
		var drawBar=false;
		if (!battler.position) return;

		var duration=1000;
		var nowTime=(new Date()).getTime();
		var alpha=Math.sin(Math.PI*(nowTime%duration/duration))*1;

		if (battler==activeBattler && activeTarget!=null)
		{}
		else
		{
			var myside=0;
			if (battler.side=="enemy") myside=1;
			if (selectSide==0 && partners[selectPointer]==battler || selectSide==1 && enemys[selectPointer]==battler || selectSide==myside && selectPointer=="all" && battler.alive)
			{
				battler.borderSize+=host.delayTime/100*64;
				if (battler.borderSize>battler.borderLimit) battler.borderSize=battler.borderLimit;
			}
			else
			{
				battler.borderSize-=host.delayTime/100*64;
				if (battler.borderSize<0) battler.borderSize=0;
			}
			drawBar=true;
		}
		host.stage.draw(battler.currentFrame,battler.currentPosition.x,battler.currentPosition.y);
		if (drawBar && battler.alive)
		{
			//画血条
			host.stage.getContext().fillStyle="rgba(255,255,255,0.6)";
			host.stage.getContext().fillRect(battler.currentPosition.x+(128-barWidth)/2,battler.currentPosition.y+barTop,barWidth,3);
			host.stage.getContext().fillStyle="rgba(255,0,0,1)";
			var hpwidth=barWidth*battler.hp/battler.maxHp;
			host.stage.getContext().fillRect(battler.currentPosition.x+(128-barWidth)/2,battler.currentPosition.y+barTop,hpwidth,3);
			//画法条
			host.stage.getContext().fillStyle="rgba(255,255,255,0.6)";
			host.stage.getContext().fillRect(battler.currentPosition.x+(128-barWidth)/2,battler.currentPosition.y+barTop+5,barWidth,3);
			host.stage.getContext().fillStyle="rgba(63,72,255,1)";
			var hpwidth=barWidth*battler.mp/battler.maxMp;
			host.stage.getContext().fillRect(battler.currentPosition.x+(128-barWidth)/2,battler.currentPosition.y+barTop+5,hpwidth,3);
			//画动作条
			if (battler.activePoint>-1)
			{
				host.stage.getContext().fillStyle="rgba(255,255,255,1)";
				var hpwidth=barWidth*battler.activePoint/1000;
			}
			else
			{
				host.stage.getContext().fillStyle="rgba(255,255,255,"+alpha+")";
				var hpwidth=barWidth;
			}
			host.stage.getContext().fillRect(battler.currentPosition.x+(128-barWidth)/2,battler.currentPosition.y+barTop+10,hpwidth,3);
			
			//画状态
			var statesLabel={"chaos":"乱","sleep":"眠","block":"封","defense":"防"};
			for (var i=0;i<battler.states.length;i++)
			{
				var left=battler.currentPosition.x+40+i*17;
				var top=battler.currentPosition.y+barTop+12;
				host.stage.write({
					content:statesLabel[battler.states[i].getName()],
					align:"center",
					color:"#fff",
					size:14,
					bold:false,
					shadow:{offsetX:1,offsetY:1,blur:1,color:"#333"}},left,top);
			}
		}
		
		//画选择框
		if (battler.borderSize>0)
		{
			var border_left=battler.currentPosition.x+64-battler.borderSize*0.5;
			var border_top=battler.currentPosition.y+64-battler.borderSize*0.5;
			host.stage.getContext().lineWidth=2.0;
			host.stage.getContext().strokeStyle="rgba(255,255,255,"+(alpha*0.8+0.2)+")";
			host.stage.getContext().strokeRect(border_left,border_top,battler.borderSize,battler.borderSize);
			var name_left=battler.currentPosition.x+64;
			var name_top=battler.currentPosition.y+6;
			host.stage.write({content:battler.name,color:"#fff",size:16,align:"center",bold:true,shadow:{offsetX:1,offsetY:1,blur:1,color:"#333"}},name_left,name_top);
		}
		
		//画附加信息
		if (!!battler.hurtInfo)
		{
			var timeTemp=((new Date()).getTime()-battler.hurtInfo.startTime);
			var duration=1000;
			var left=battler.currentPosition.x+64;
			var top=battler.currentPosition.y-timeTemp/duration*50+32;
			var alpha=1-timeTemp/duration;
			var damage=battler.hurtInfo.damage;
			if (battler.hurtInfo.type=="hurt")
			{
				var damageColor="rgba(255,0,0,"+alpha+")";
			}
			if (battler.hurtInfo.type=="heal")
			{
				var damageColor="rgba(0,255,0,"+alpha+")";
			}
			if (battler.hurtInfo.type=="state")
			{
				var damageColor="rgba(255,228,0,"+alpha+")";
			}
			if (alpha>0)
			{
				host.stage.write({content:damage,align:"center",color:damageColor,size:20,bold:true},left,top);
			}
		}
		
		if (step=="victory")
		{
				host.stage.write({
					content:"战斗胜利！",
					align:"center",
					baseLine:"middle",
					color:"#fff",
					bold:true,
					shadow:{offsetX:1,offsetY:1,blur:1,color:"#333"},
					size:48},
					host.config.width*0.5,host.config.height*0.5);
			if(victory&&battler.side=="partner")
			{
		    var timeTemp=((new Date()).getTime()-victoryTime);
			var duration=2000;
			var left=battler.currentPosition.x+64;
			var top=battler.currentPosition.y-timeTemp/duration*50+32;
			var alpha=1-timeTemp/duration;
			if (alpha>0)
			{
				host.stage.write({content:"获得"+totalExperience+"点经验值"+(battler.upgrade?" 升级了！":"")
				,align:"center",color:"rgba(255,255,255,"+alpha+")",size:20,bold:true},left,top);
			 }
			}
		}
		
		if (step=="defeated")
		{
				host.stage.write({
					content:"战斗失败！",
					align:"center",
					baseLine:"middle",
					color:"#fff",
					bold:true,
					shadow:{offsetX:1,offsetY:1,blur:1,color:"#333"},
					size:48},
					host.config.width*0.5,host.config.height*0.5);
		}
	}
	
	this.drawEffect=function()           //绘制特效
	{
		for (var i=0;i<effects.length;i++)
		{
			host.stage.draw(effects[i].frame,effects[i].left,effects[i].top);
		}
	}
	
	this.refresh=function()
	{
		effects.splice(0);  //清空
		this.update();
		host.stage.clear(background);
		for (var i=0;i<enemys.length;i++)
		{
			this.drawBattler(enemys[i]);      
		}
		for (var i=0;i<partners.length;i++)
		{
			this.drawBattler(partners[i]);
		}
		this.drawEffect();
		if (activeDialog!=null)
		{
			activeDialog.drawSelf();
		}
	}
}

OS_Resource.Builder.put("battle",OS_Battle);